//
//  UIImage+color.m
//  HKTimeKitApp
//
//  Created by huzhaoke1 on 2023/5/9.
//

#import "UIImage+color.h"

@implementation UIImage (color)

+ (instancetype)imageWithCtxSize:(CGSize)ctxSize
                       imageRect:(CGRect)rect
                        color:(UIColor *)color
                      corners:(UIRectCorner)corners
                 cornerRadius:(CGFloat)radius {
    UIGraphicsBeginImageContextWithOptions(ctxSize, NO, 0);
    
    UIBezierPath *path;
    if (radius > 0) {
        path = [UIBezierPath bezierPathWithRoundedRect:rect byRoundingCorners:corners cornerRadii:CGSizeMake(radius, radius)];
    } else {
        path = [UIBezierPath bezierPathWithRect:rect];
    }
    
    if (color) {
        [color setFill];
        [path fill];
    }
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    if (@available(iOS 13.0, *)) {
        color = [color colorWithAlphaComponent:1.0];
        image = [image imageWithTintColor:color];
    }
    
    return image;
}


@end
